#include <stdio.h>
#include <stdlib.h>

#include "llist.h"

#define NAMESIZE 32
struct score_st
{
	int id;
	char name[NAMESIZE];
	int math;
	int chinese;
};
static void print_s(const void *record)
{
	const struct score_st *r = record;/* 转换*/
	printf("%d %s %d %d\n",r->id,r->name,r->math,r->chinese);
	return ;
}

static int id_cmp(const void *key,const void *record)
{
	const int *k = key;
	const struct score_st *r = record;

	return (*k - r->id);
}


static int name_cmp(const void *key,const void *record)
{
	const char *k=key;
	const struct score_st *r = record;
	
	return strcmp(k,r->name);
}



int main()
{
	LLIST *handler;
	struct score_st tmp;	
	handler = llist_create(sizeof(struct score_st));
	if (handler == NULL)
		exit(1);
	int i=0,ret = 0;
	for (i=0;i<7;i++)
	{		
		tmp.id = i;
		snprintf(tmp.name,NAMESIZE,"std%d",i);
		tmp.math = rand()%100;
		tmp.chinese = rand()%100;
		ret = llist_insert(handler,&tmp,LLIST_BACKWARD);
		if (ret != 0)
		{
			if(ret == -1)
				fprintf(stderr,"malloc newnode error!.\n");
			else if(ret == -2)
				fprintf(stderr,"malloc newnode->data error!.\n");
			else if(ret == -3)
				fprintf(stderr,"llist_insert input value error.\n");
			else
				fprintf(stderr,"ERROE!\n");

			exit(1);
		}
	}
	llist_travel(handler,print_s);
	printf("\n\n");
#if 0
/*根据id进行查找测试例子*/
	int id = 30;
	struct score_st *score_temp_find_test;

	score_temp_find_test = llist_find(handler,&id,id_cmp);

	if (score_temp_find_test == NULL)
		printf("Can not find!\n");
	else
		print_s(score_temp_find_test);	
#endif

#if 0 
/*根据id进行删除测试例子*/
	int id = 3;
	ret = llist_delete(handler,&id,id_cmp);
	if (ret == -1)
		printf("Can not find!\n");
	llist_travel(handler,print_s);
#endif

#if 0
/*根据name进行删除的测试用例*/

	char *del_name = "std2";

	ret = llist_delete(handler,del_name,name_cmp);
	if (ret == -1)
		printf("Can not find!\n");
	llist_travel(handler,print_s);
#endif

#if 0
	char *del_name = "std2";
	void *data = NULL;
	ret = llist_fetch(handler,del_name,name_cmp,data);

	if(ret == -1)
		printf("Can not find!\n");

//	print_s(data);//段错误
	llist_travel(handler,print_s);
#endif

	llist_destroy(handler);


	exit(0);
}
